/**
 * https://codeforces.com/problemset/problem/1980/E
 */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin>>t;
	while(t--){
		ll n,m;
		cin>>n>>m;
		vector<vector<ll>> r(n,vector<ll>(m));
		vector<vector<ll>> l(n,vector<ll>(m));
		set<set<ll>> v;
		set<set<ll>> st;
		set<ll>p;
		for(int i=0;i<n;++i){
			p.clear();
			for(int j=0;j<m;++j){
				cin>>r[i][j];
				p.insert(r[i][j]);
			}
			v.insert(p);
   	    }
		for(int i=0;i<n;++i){
			p.clear();
			for(int j=0;j<m;++j){
				cin>>l[i][j];
				p.insert(l[i][j]);
			}
			st.insert(p);
   	    }
   	    if(v==st){
   	    set<set<ll>> vp;
		set<set<ll>> sp;
   	    for(int i=0;i<m;++i){
   	    	set<ll> h;
   	    	h.clear();
			p.clear();
   	    	for(int j=0;j<n;j++){
   	    		h.insert(r[j][i]);
   	    		p.insert(l[j][i]);
			}
			vp.insert(h);
			sp.insert(p);
	    }
	    if(vp==sp)
   	    cout<<"YES"<<endl;
   	    else
   	    cout<<"NO"<<endl;
	    }
   	    else
   	    cout<<"NO"<<endl;
	}
	return 0;
}